﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MyVoteMVC.Data;
using MyVoteMVC.Domain;
using MyVoteMVC.Common;
using MyVoteMVC.Common.Objects;
using MyVoteMVC.Data.Repositories;

namespace MyVoteMVC.Service
{
    public class VoteQuestionService : IQuestionService
    {
        private const string EmailAddress = "@gzdi.com";//接收方邮箱    
        IUnitOfWork unitOfWork;
        IRepository<VT_Questions> questionsRepository;
        IRepository<VT_Activities> activitesRepostory;
        
        public VoteQuestionService()
        {
            unitOfWork = Factory.GetInstance<IUnitOfWork>();
            questionsRepository = unitOfWork.GetRepository<VT_Questions>();
            activitesRepostory = unitOfWork.GetRepository<VT_Activities>();
        }
          
        //增加
        public bool CreateQuestion(string activityID, string questionTitle, string questionContent, int IfMustAnswer, int IfMultiItems, string userEmailName,int mCount)
        {
            //userEmailName = userEmailName + EmailAddress;
            VT_Questions v_Questions = new VT_Questions();
            unitOfWork.BeginTransaction();
            v_Questions.Activity_ID = activityID;
            v_Questions.Created_By = userEmailName;
            v_Questions.Created_Date = DateTime.Now;
            v_Questions.Question_Content = questionContent;
            v_Questions.Question_ID = Guid.NewGuid().ToString();
            int msx = Convert.ToInt32((from activity in questionsRepository.QueryableEntity()
                                       where activity.Activity_ID == activityID
                                       select activity.Question_No).Max());
            v_Questions.Question_No = msx + 1 ;
            v_Questions.Question_Title = questionTitle;
            v_Questions.Question_IfMustAnswer = IfMustAnswer;
            v_Questions.Question_IfMultiItems = IfMultiItems;
            v_Questions.Question_Status = 1;
            v_Questions.MultiCount = mCount;
            questionsRepository.Add(v_Questions);
            unitOfWork.CommitTransaction();
            return true;
        }

        //编辑
        public bool EditQuestion(string activityID, string questionID, string questionTitle, string questionContent, int IfMustAnswer, int IfMultiItems, int questionStatus, string userEmailName,int mCount)
        {
            //userEmailName = userEmailName + EmailAddress;
            VT_Questions v_Questions = questionsRepository.GetMany(o => o.Activity_ID == activityID && o.Question_ID == questionID).FirstOrDefault();
            if (v_Questions != null)
            {
                unitOfWork.BeginTransaction();
                v_Questions.Question_Content = questionContent;
                v_Questions.Question_Title = questionTitle;
                v_Questions.Question_IfMustAnswer = IfMustAnswer;
                v_Questions.Question_IfMultiItems = IfMultiItems;
                v_Questions.Question_Status = questionStatus;
                v_Questions.Last_Update_By = userEmailName;
                v_Questions.Last_Update_Date = DateTime.Now;
                v_Questions.MultiCount = mCount;
                questionsRepository.Update(v_Questions);  
                unitOfWork.CommitTransaction();
                return true;
            }
            return false;
        }
        //删除
        public bool DeleteQuestion(string activityID, string questionID)
        {
            VT_Questions v_Questions = questionsRepository.GetMany(o => o.Activity_ID == activityID && o.Question_ID == questionID).FirstOrDefault();
            if (v_Questions != null)
            {
                unitOfWork.BeginTransaction();
                questionsRepository.Delete(v_Questions);
                unitOfWork.CommitTransaction();
                return true;
            }
            return false;
        }

        public IList<VT_Questions> GetQuestions(string activityID)
        {
            return questionsRepository.GetMany(o => o.Activity_ID == activityID).OrderBy(o => o.Question_No).ToList();
        }


        public VT_Questions GetQuestion(string questionID)
        {
            return questionsRepository.GetEntityById(questionID);
        }
        
        //判断当前问题是否属于二次活动
        public bool GetQuestionLastActivites(string questionID)
        {
            var v_question = from v_questions in questionsRepository.QueryableEntity()
                              join activites in activitesRepostory.QueryableEntity() on v_questions.Activity_ID equals activites.Activity_ID
                              where activites.Activity_PID != null || activites.Activity_PID != string.Empty
                              select v_questions;
            if (v_question != null)
                return true;

            return false;
        }



        #region IDisposable Members
        private bool m_disposed;
        protected virtual void Dispose(bool disposing)
        {
            if (!m_disposed)
            {
                if (disposing)
                {
                    unitOfWork.Dispose();   
                }

                // Release unmanaged resources

                m_disposed = true;
            }
        }

        public void Dispose()
        {
            Dispose(true);
            GC.SuppressFinalize(this);
        }

        ~VoteQuestionService()
        {
            Dispose(false);
        }
        #endregion


     
    }
}
